﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class UICurrencyCollect : MonoBehaviour
{
    private float t1 = 0.2f;//第一段飞行时间
    private float t2 = 0.4f;//第二段飞行时间
    private Transform targetPos;
    private Vector3 _startPos1;
    private Vector3 _endPos;
    private Vector3[] _v_middle = new Vector3[] { new Vector3(0, 0, 0) };
    private Vector3[] _v_end = new Vector3[] { new Vector3(0, 0, 0) };
    private CanvasGroup cg;
    private void Start()
    {
        cg = GetComponent<CanvasGroup>();
        //InitAnimation(transform.gameObject);
    }

    public void InitAnimation(Transform tartgePosFinal)
    {
        _startPos1 = transform.position;
        _endPos = tartgePosFinal.position;
        //Debug.Log("tartgePosFinal.position :" + tartgePosFinal.position);
        this.transform.SetAsLastSibling();
        transform.parent.transform.SetAsLastSibling();
        //transform.parent.parent.transform.SetAsLastSibling();
        float random_x, random_y;
        random_x = Random.Range(-200, 250);
        //Debug.Log("random_x = " + random_x);
        random_y = Random.Range(50, 250);
        _v_middle[0] = _startPos1 + new Vector3(random_x, random_y, 0);
        _v_end[0] = _endPos;
        Sequence _seq = DOTween.Sequence();
        _seq.Append(transform.DOPath(_v_middle, t1, PathType.CatmullRom).SetEase(Ease.Linear));
        _seq.AppendInterval(0.1f);
        _seq.Append(transform.DOPath(_v_end, t2, PathType.CatmullRom).SetEase(Ease.Linear));
        _seq.Join(DOTween.To(() => cg.alpha, x => cg.alpha = x, 0, 0.5f));
        _seq.AppendCallback(delegate { GameObject.Destroy(this.gameObject); });
    }

    public void SetFlyParam(float mTime)
    {
        t1 = mTime * 0.3f;
        t2 = mTime * 0.4f;
    }
}